export default function useLoading<T>(useMiniUI?: any, opts?: {loading: string, fail?: (e: Error) => void, success?: (r: T) => any}) {
  return async (process: () => Promise<T>) => {
    const miniui = await useMiniUI()
    const index = miniui.loading(opts && opts.loading, "")
    try {
      let result = await process()
      if (opts && opts.success && typeof opts.success === 'function') {
        try {
          result = opts.success(result) || result
        }
        catch (e) {}
      }

      return result
    }
    catch (e) {
      opts && opts.fail && typeof opts.fail === 'function' && opts.fail(e)
    }
    finally {
      miniui.hideMessageBox(index)
    }
  }
}